package Servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/instructorAdmin")
public class instructorAdmin extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		if(request.getParameter("edit")!=null)
		{
			System.out.println("Admin Instructor->Edit");
			String id= request.getParameter("empid");
			String fname= request.getParameter("fName");
			String lname= request.getParameter("lName");
			String address= request.getParameter("address");
			String city= request.getParameter("city");
			String state= request.getParameter("state");;
			String zipcode= request.getParameter("zipcode");
			String department = request.getParameter("department");
			String location = request.getParameter("location");
			String hours = request.getParameter("officeHours");
			
			request.setAttribute("empid", id);
			request.setAttribute("fname", fname);
			request.setAttribute("lname", lname);
			request.setAttribute("address", address);
			request.setAttribute("city", city);
			request.setAttribute("state", state);
			request.setAttribute("zipcode", zipcode);
			request.setAttribute("department", department);
			request.setAttribute("location", location);
			request.setAttribute("hours", hours);
			
			RequestDispatcher rd=getServletContext().getRequestDispatcher("/View/instructorInfo.jsp");
			rd.forward(request, response);
		}
		else
		{
			if(request.getParameter("delete")!=null)
			{
				String id= request.getParameter("empid");
				String fname= request.getParameter("fName");
				String lname= request.getParameter("lName");				
				System.out.println("Admin Instructor->Delete "+id+" "+fname+" "+lname+".");
				
				//DELETE FROM DB
			}
			else
			{
				if(request.getParameter("assign")!=null)
				{
					System.out.println("Admin Instructor->Assign");
					//ACCESS DB TO GET COURSES
					String id = request.getParameter("empid");
					request.setAttribute("empid", id);
					
					String name = request.getParameter("fName")+" "+request.getParameter("lName");
					request.setAttribute("name", name);
					
					RequestDispatcher rd=getServletContext().getRequestDispatcher("/View/assignCourse.jsp");
					rd.forward(request, response);
				}
				else
				{
					if(request.getParameter("remove")!=null)
					{
						System.out.println("Admin Instructor->Remove");
						
						String name = request.getParameter("fName")+" "+ request.getParameter("lName");
						request.setAttribute("name", name);
						String id = request.getParameter("empid");
						request.setAttribute("id", id);
						String courses = request.getParameter("courses");
						String[] courselist = courses.split(",");
						ArrayList<String> courselist1 = new ArrayList<String>();
						for(String s:courselist)
							courselist1.add(s.trim());
						request.setAttribute("courses",courselist1);
						RequestDispatcher rd=getServletContext().getRequestDispatcher("/View/removeCourse.jsp");
						rd.forward(request, response);
					}
				}
			}
			
		}
	}

}
